package what.the.fun.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import what.the.fun.domain.Answer;
import what.the.fun.domain.QuestAnsList;
import what.the.fun.domain.Question;
import what.the.fun.services.AnswerService;
import what.the.fun.services.QuestionService;

@Controller
public class QuestionController {
	
	@Autowired
	QuestionService questionService;
	@Autowired
	AnswerService answerService;
	
	ModelAndView loginMaV = new ModelAndView("/user/login");
	
	@RequestMapping("/quest-create")
	public ModelAndView createQuest(HttpSession session, @ModelAttribute QuestAnsList questAnsList
			, String surveyId) {
		if (session.getAttribute("userId") != null) {
		
			Map<String, String> map = new HashMap<String, String>();
			map.put("surveyId", surveyId);
			
			return new ModelAndView("question/quest-create", "map", map);
		} else {
			 return loginMaV;
		 }
	}
	
	@RequestMapping(value="/quest-insert", produces="application/x-www-form-urlencoded;charset=UTF8")
	public String insertQuest(HttpSession session, @ModelAttribute QuestAnsList questAnsList, String surveyId) {
		if (session.getAttribute("userId") != null) {
			try {
				// insert question
				questAnsList.getQuestion().setSurveyId(surveyId);
				//questAnsList.getQuestion().setLimitAnswer(0); 			// set limit answer
				questionService.insertData(questAnsList.getQuestion());
				
				// insert answers
				String questId = questionService.getLast().getQuestId();
				for(Answer answer : questAnsList.getAnswerList()) {
					answer.setQuestId(questId);
					answerService.insertData(answer);
				}
				
				return "redirect:/survey-detail?surveyId=" + surveyId;
			} catch (Exception e) {
				System.out.println(e.toString());
				return "redirect:/error";
			}
		} else {
			 return "redirect:/login";
		 }
	}
	
	@RequestMapping(value="/quest-detail")
	public ModelAndView detailQuest(HttpSession session, @ModelAttribute QuestAnsList questAnsList,
									@RequestParam String questId) {
		if (session.getAttribute("userId") != null) {
			List<Answer> answerList = new ArrayList<Answer>();
			answerList = answerService.getAnswerListByQuestId(questId);
			ModelMap model = new ModelMap();
			model.put("answerList", answerList);
			
			Question question = questionService.getData(questId);
			model.addAttribute("question", question);
			
			String surveyId = question.getSurveyId();
			model.addAttribute("surveyId", surveyId);
			
			return new ModelAndView("question/quest-detail", "model", model);
		}else {
			 return loginMaV;
		 }
	}
	
	@RequestMapping("/quest-update")
	public String updateQuest(HttpSession session, @ModelAttribute QuestAnsList questAnsList,
									@RequestParam String questId) {
		if (session.getAttribute("userId") != null) {
			try {
				Question question = questionService.getData(questAnsList.getQuestion().getQuestId());
				question.setQuestContent(questAnsList.getQuestion().getQuestContent());
				question.setLimitAnswer(questAnsList.getQuestion().getLimitAnswer());
				
				questionService.updateData(question);
				if (questAnsList.getAnswerList() != null) {
					for(Answer answer : questAnsList.getAnswerList()) {
						answer.setQuestId(questId);
						if (answer.getAnswerId() != null) {
							answerService.updateData(answer);
						} else {
							answerService.insertData(answer);
						}
						
					}
				}
				String surveyId = question.getSurveyId();
				 
				return "redirect:/survey-detail?surveyId=" + surveyId;
			} catch (Exception e) {
				System.out.println(e.toString());
				return "redirect:/error";
			}
		}
		else {
			 return "redirect:/login";
		 }
		
	}
	
	@RequestMapping("/quest-add-answer")
	public String addAnswerQuest(HttpSession session, @ModelAttribute QuestAnsList questAnsList2,
									@RequestParam String questId) {
		if (session.getAttribute("userId") != null) {
			try {
				for(Answer answer : questAnsList2.getAnswerList()) {
					answer.setQuestId(questId);
					answerService.insertData(answer);
				}
				return "redirect:/quest-detail?questId=" + questId;
			} catch(Exception e) {
				System.out.println(e.toString());
				return "redirect:/error";
			}
		}else {
			 return "redirect:/login";
		 }
	}
}
